<template>
  <div class="container">
    <div class="header" style="display: inline-block">
      <el-button
        icon="el-icon-plus"
        type="primary"
        plain
        @click="handleOpenDialog('1')"
        >新增菜品分类</el-button
      >

      <el-button
        icon="el-icon-plus"
        type="primary"
        @click="handleOpenDialog('2')"
        >新增套餐分类</el-button
      >
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="分类名称"> </el-table-column>
      <el-table-column prop="type" label="分类类型">
        <template v-slot="{ row }">
          {{ row.type === 1 ? "菜品分类" : "套餐分类" }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="操作时间"> </el-table-column>
      <el-table-column prop="sort" label="排序"> </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="handleEdit(row)">修改</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="此操作将永久删除该文件, 是否继续?"
            @onConfirm="handleChange(row.id)"
          >
            <el-link type="danger" slot="reference"> 删除</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      :title="`${form.id ? '编辑' : '新增'}${
        form.type === '1' ? '菜品' : '套餐'
      }分类`"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="form">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input
            v-model="form.name"
            autocomplete="off"
            placeholder="请输入分类名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" :label-width="formLabelWidth">
          <el-input
            v-model="form.sort"
            autocomplete="off"
            placeholder="请输入排序"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="warning" plain @click="handleSure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCategory,
  deleteCategory,
  addCategory,
  editCategory,
} from "@/api/category";
export default {
  data() {
    return {
      tableData: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      dialogFormVisible: false,
      form: {
        name: "",
        sort: "",
        type: "1",
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getCategory(this.pageInfo).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleChange(row) {
      deleteCategory({ id: row.id }).then((res) => {
        this.$message.success("删除成功");
        this.init();
      });
    },
    handleSizeChange(val) {
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    handleOpenDialog(type) {
      this.form.type = type;
      this.dialogFormVisible = true;
    },
    handleSure() {
      if (this.form.id) {
        editCategory(this.form).then((res) => {
          this.$message.success("编辑成功");
          this.dialogFormVisible = false;
          this.form = {};
          this.init();
        });
      } else {
        addCategory(this.form).then((res) => {
          this.$message.success("添加成功");
          this.dialogFormVisible = false;
          this.form = {};
          this.init();
        });
      }
    },
    handleEdit(row) {
      this.form = row;
      this.dialogFormVisible = true;
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 30px;
}

.el-form {
  margin-top: 50px;
  width: 500px;
}
</style>
